package be.kuleuven.peno3.mobiletoledo.view;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import be.kuleuven.peno3.mobiletoledo.R;
import be.kuleuven.peno3.mobiletoledo.Data.UserDAO;
import be.kuleuven.peno3.mobiletoledo.Data.Client.CredentialsClient;

public class InsertUserData extends Activity {
	private EditText username, userpassword, password, group, userid;
	private Spinner studyprogramme;
	private Button submit;
	
	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.insertuserdatalayout);
		
		TextView bar = (TextView) findViewById(R.id.reg_bar);
		bar.setText("Registratie");
		
		studyprogramme = (Spinner) findViewById(R.id.studyprogrammeselector);
		
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.studyprogrammes, android.R.layout.simple_spinner_item);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    studyprogramme.setAdapter(adapter);
	    
		username = (EditText) findViewById(R.id.insertuserdata_username);
		userpassword = (EditText) findViewById(R.id.insertuserdata_userpassword);
		password = (EditText) findViewById(R.id.insertuserdata_campusnetpassword);
		group = (EditText) findViewById(R.id.insertuserdata_group);
		userid = (EditText) findViewById(R.id.insertuserdata_userid);
		submit = (Button) findViewById(R.id.insertuserdata_submit);
		// To set all fields when editing settings.
		UserDAO userManager = new UserDAO(InsertUserData.this.getApplicationContext());
		userManager.open();
		if (userManager.fetchProperty("username") != null){
			username.setText(userManager.fetchProperty("username"));
			userpassword.setText(userManager.fetchProperty("userpassword"));
			password.setText(userManager.fetchProperty("password"));
			group.setText(userManager.fetchProperty("group"));
			userid.setText(userManager.fetchProperty("userid"));
			String[] stdyprgm = getResources().getStringArray(R.array.studyprogrammes);
			boolean go = true;
			for (int i = 0; i < stdyprgm.length | go; i++){
				if (stdyprgm[i].equals(userManager.fetchProperty("studyprogramme"))){
					go = false;
					studyprogramme.setSelection(i);
				}
			}
		}
		userManager.close();
		
		submit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				UserDAO userdao = new UserDAO(InsertUserData.this.getApplicationContext());
				if(username.getText().toString().equals("") || password.getText().toString().equals("") || userpassword.getText().toString().equals("") || group.getText().toString().equals("") || userid.getText().toString().equals("")) {
					Toast.makeText(InsertUserData.this, "Gelieve alle velden in te vullen", Toast.LENGTH_SHORT).show();
					return;
				}
				
				String status = CredentialsClient.testCredentials(userid.getText().toString(), userpassword.getText().toString());
				boolean lesgever=false;
				if(status.equals("true")) { // ingelogd, geen lesgever
					lesgever=true;
				}
				else if (status.equals("false")) { //ingelogd, lesgever
					lesgever=false;
				}
				else { // niet ingelogd
					Toast.makeText(InsertUserData.this, "Gelieve een juiste gebruikersnaam en wachtwoord in te vullen", Toast.LENGTH_SHORT).show();
					return;
				}
				
				
				userdao.open();
				userdao.truncate();
				userdao.createEntry("teacher", ""+lesgever);
				userdao.createEntry("username", username.getText().toString());
				userdao.createEntry("userpassword", userpassword.getText().toString());
				userdao.createEntry("password", password.getText().toString());
				userdao.createEntry("group", group.getText().toString().toLowerCase());
				userdao.createEntry("userid", userid.getText().toString().toLowerCase());
				userdao.createEntry("studyprogramme", studyprogramme.getSelectedItem().toString().toLowerCase());
				userdao.close();
				Toast.makeText(InsertUserData.this, "Data opgeslagen", Toast.LENGTH_SHORT).show();
				finish();
				return;
			}
		});
		
	}
	
	
}
